<?php 

class ShopModel extends Model{
	//指定当前模型所对应的表
	public $table = "shop";
	
	//自动验证
	public $validate=array(
       array('sname','nonull','商品名称不能为空',2,3),
	);	
	
	//添加商品
	public function addData(){
		//自动验证
		if(!$this->create()){
			return false;
		}
		if(IS_POST){
			$shop = Q('post.');
			//找到该条数据分类id(cid)
			$cid = $shop['category_cid'];
			//查询该cid所对应的类型id(tid)
			$tid = K('Category')->field('type_tid')->where("cid={$cid}")->find();
			$tid = $tid['type_tid'];
			
			//重组商品表数组
			$shopStr = array(
				'sname'	=>	$shop['sname'],
				'unit'	=>	$shop['unit'],
				'marketprice'	=>	$shop['marketprice'],
				'shopprice'	=>	$shop['shopprice'],
				'time'	=>	time(),
				'category_cid'	=>	$shop['category_cid'],
				'type_tid'	=>	$tid,
				'admin_aid'	=>	$_SESSION['aid'],
				'brand_bid'	=>	$shop['brand_bid'],	
				'pic'	=>	$shop['pic'],		
			);
			//添加数据到商品表	
			$sid = $this->add($shopStr);	
			
			//重组商品属性表数组 
			//找到post里的商品属性
			$attr = Q('post.attr');
			foreach ($attr as $k => $v) {
				$shopAttr = array(
					'type_attr_ta_id'	=>	$k,
					'savalue'	=>	$v,
					'shop_sid'	=>	$sid,
				);
				//添加商品属性到商品属性表
				K('ShopAttr')->add($shopAttr);
			}
			//找到post里的商品规格
			$spec = $shop['spec'];
			foreach ($spec as $key => $value) {
				foreach ($value['spec_val'] as $k => $v) {
					$shopSpec = array(
					     'savalue'=>$v,
					     'added'=>$value['added'][$k],
					     'type_attr_ta_id'=>$key,
					     'shop_sid' => $sid,					
					);
					//添加商品规格到商品属性表
					K('ShopAttr')->add($shopSpec);
				}
			}
			
			//重组商品详情表数组
			if (isset($shop['thumb'])) {
			    $thumb = array();
				foreach ($shop['thumb'] as $v) {
					if (strstr($v,'big')) {
					    $thumb ['big'][] = $v;
					}
					if (strstr($v,'medium')) {
					    $thumb['medium'][]=$v;
					}
					if (strstr($v,'small')) {
					    $thumb['small'][]=$v;
					}
				}
				//将图片地址变为字符串
				$detailArr = array(
						'small' => implode(',', $thumb['small']),
						'medium' => implode(',', $thumb['medium']),
						'big' => implode(',', $thumb['big'])
					);
				$detailArr['intro'] = $shop['intro'];
				$detailArr['shop_sid'] = $sid;
				//添加数据到商品详情表
				K('ShopDetail')->add($detailArr);				
			}
		}		
	}

	//修改商品
	public function editData(){
		//自动验证
		if(!$this->create()){
			return false;
		}
		if(IS_POST){
			$shop = Q('post.');
			//获得该条数据的sid
			$sid = Q('post.sid',0,'intval');
			//找到该条数据分类id(cid)
			$cid = $shop['category_cid'];
			//查询该cid所对应的类型id(tid)
			$tid = K('Category')->field('type_tid')->where("cid={$cid}")->find();
			$tid = $tid['type_tid'];
			
			//重组商品表数组
			$shopStr = array(
				'sname'	=>	$shop['sname'],
				'unit'	=>	$shop['unit'],
				'marketprice'	=>	$shop['marketprice'],
				'shopprice'	=>	$shop['shopprice'],
				'time'	=>	time(),
				'category_cid'	=>	$shop['category_cid'],
				'type_tid'	=>	$tid,
				'admin_aid'	=>	$_SESSION['aid'],
				'brand_bid'	=>	$shop['brand_bid'],	
				'pic'	=>	$shop['pic'],		
			);
			//修改数据到商品表	
			$this->where("sid={$sid}")->update($shopStr);
			
			//重组商品属性表数组 
			//找到post里的商品属性
			$attr = Q('post.attr');
			foreach ($attr as $k => $v) {
				$shopAttr = array(
					'type_attr_ta_id'	=>	$k,
					'savalue'	=>	$v,
					'shop_sid'	=>	$sid,
				);
				//添加商品属性到商品属性表
				K('ShopAttr')->where("shop_sid={$sid}")->update($shopAttr);
			}
			//找到post里的商品规格
			$spec = $shop['spec'];
			foreach ($spec as $key => $value) {
				foreach ($value['spec_val'] as $k => $v) {
					$shopSpec = array(
					     'savalue'=>$v,
					     'added'=>$value['added'][$k],
					     'type_attr_ta_id'=>$key,
					     'shop_sid' => $sid,					
					);
					//添加商品规格到商品属性表
					K('ShopAttr')->where("shop_sid={$sid}")->update($shopSpec);
				}
			}
			
			//重组商品详情表数组
			if (isset($shop['thumb'])) {
			    $thumb = array();
				foreach ($shop['thumb'] as $v) {
					if (strstr($v,'big')) {
					    $thumb ['big'][] = $v;
					}
					if (strstr($v,'medium')) {
					    $thumb['medium'][]=$v;
					}
					if (strstr($v,'small')) {
					    $thumb['small'][]=$v;
					}
				}
				//将图片地址变为字符串
				$detailArr = array(
						'small' => implode(',', $thumb['small']),
						'medium' => implode(',', $thumb['medium']),
						'big' => implode(',', $thumb['big'])
					);
				$detailArr['intro'] = $data['intro'];
				$detailArr['shop_sid'] = $sid;
				//添加数据到商品详情表
				K('ShopDetail')->add($detailArr);				
			}
		}	   
	}	
}

 ?>